﻿using CommunityToolkit.Mvvm.ComponentModel;
using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media;
using WpfMvvmTemplate.MD.Common;
using WpfMvvmTemplate.MD.Models.BindingModels;

namespace WpfMvvmTemplate.MD.ViewModels.Windows
{
    public partial class SkinWindowViewModel : ObservableObject
    {
        public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;

        private readonly PaletteHelper paletteHelper = new();

        public SkinWindowViewModel()
        {
            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);

            string selectedBrushValue = jObject?["AppSettings"]?["SelectedBrush"]?.Value<string>()!;
            SolidColorBrush selectedBrush = new ((Color)ColorConverter.ConvertFromString(selectedBrushValue));
            SelectedColor = selectedBrush.Color;

        }

        /// <summary>
        /// 选择的颜色
        /// </summary>
        [ObservableProperty]
        public Color? selectedColor;

        partial void OnSelectedColorChanged(Color? value)
        {
            var hue = (Color)value!;
            ITheme theme = paletteHelper.GetTheme();
            theme.PrimaryLight = new ColorPair(hue.Lighten());
            theme.PrimaryMid = new ColorPair(hue);
            theme.PrimaryDark = new ColorPair(hue.Darken());
            paletteHelper.SetTheme(theme);
        }
    }
}